package com.itheima.day02;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        //1、建立连接: 使用Socket创建客户端 +服务的地址和端口
        Socket client = new Socket("localhost",8888);
        System.out.println("连接服务器......");

        byte[] data = new byte[10000];
        BufferedInputStream str = new BufferedInputStream(client.getInputStream());
        int new_str = str.read(data);

        Class<?> c1 = new MyClassLoader().load(data, new_str);
        Object obj = c1.newInstance();

        int[] asd = {9,8,100,80,54,31,88};
        Method bubbleSort = c1.getDeclaredMethod("bubbleSort", int[].class);
        bubbleSort.invoke(obj, (Object) asd);
    }

    private static class MyClassLoader extends ClassLoader {
        public Class<?> load(byte[] data,int length){
            return super.defineClass(null, data, 0, length);
        }
    }
}
